package com.tianqu.tms.www.statistics.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.www.statistics.dao.TmsMarketCarPriceDayCountDao;
import com.tianqu.tms.www.statistics.dto.CarPriceLibraryDto;
import com.tianqu.tms.www.statistics.dto.CarPriceLibraryInnerDto;

public class InventoryCarPriceService {

	
	/**
	 * 车价统计-在库
	 * @return
	 * @throws TmsDaoException
	 */
	public CarPriceLibraryDto getCarPriceLibrary(String startDate, String endDate) throws TmsDaoException{

		CarPriceLibraryDto carPriceLibraryDto = new CarPriceLibraryDto();

		// 库存平均车价商户TOP5
		TmsMarketCarPriceDayCountDao tmsMarketCarPriceDayCountDao = new TmsMarketCarPriceDayCountDao();
		
		Map<String, CarPriceLibraryInnerDto> carPriceLibrary = tmsMarketCarPriceDayCountDao.getCarsPriceLibrary(startDate, endDate);
		
		this.between2Date(startDate, endDate, carPriceLibrary, carPriceLibraryDto);
		
		return carPriceLibraryDto;
	}
	
	/**
	 * 两个日期之间的所有日期列表
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	private void between2Date(String startDate, String endDate, Map<String, CarPriceLibraryInnerDto> carPriceLibrary, CarPriceLibraryDto carPriceLibraryDto) {
		
		// 日期列表（X轴）
		ArrayList<String> dateList = new ArrayList<String>();
		
		// 5万以下
		ArrayList<String> price1List = new ArrayList<String>();
		
		// 5~8万
		ArrayList<String> price2List = new ArrayList<String>();
		
		// 8~10万
		ArrayList<String> price3List = new ArrayList<String>();
		
		// 10~15万
		ArrayList<String> price4List = new ArrayList<String>();
		
		// 15~20万
		ArrayList<String> price5List = new ArrayList<String>();
		
		// 20~25万
		ArrayList<String> price6List = new ArrayList<String>();
		
		// 25~40万
		ArrayList<String> price7List = new ArrayList<String>();
		
		// 40万以上
		ArrayList<String> price8List = new ArrayList<String>();
		
		carPriceInfoByDate(startDate, carPriceLibrary, dateList, price1List,price2List,price3List,price4List,price5List,price6List,price7List,price8List);
		
		try {
			Date start = DateUtil.string102ToDate(startDate);
			Date end = DateUtil.string102ToDate(endDate);
			

			// 如果start <= end
			while(!DateUtil.compareDate102(start, end)) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(start);
				// 加一天
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				
				start = calendar.getTime();
				
				String tmpDate = DateUtil.DateToString11(start);
				
				carPriceInfoByDate(tmpDate, carPriceLibrary, dateList, price1List,price2List,price3List,price4List,price5List,price6List,price7List,price8List);
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		carPriceLibraryDto.setDateList(dateList);
		carPriceLibraryDto.setPrice1List(price1List);
		carPriceLibraryDto.setPrice2List(price2List);
		carPriceLibraryDto.setPrice3List(price3List);
		carPriceLibraryDto.setPrice4List(price4List);
		carPriceLibraryDto.setPrice5List(price5List);
		carPriceLibraryDto.setPrice6List(price6List);
		carPriceLibraryDto.setPrice7List(price7List);
		carPriceLibraryDto.setPrice8List(price8List);
	}
	
	private void carPriceInfoByDate(String strDate, 
			Map<String, CarPriceLibraryInnerDto> CarPriceLibrary,
			ArrayList<String> dateList, 
			ArrayList<String> price1List, 
			ArrayList<String> price2List,
			ArrayList<String> price3List,
			ArrayList<String> price4List,
			ArrayList<String> price5List,
			ArrayList<String> price6List,
			ArrayList<String> price7List,
			ArrayList<String> price8List){
		
		dateList.add(strDate);
		
		CarPriceLibraryInnerDto carPriceLibraryInnerDto1 = CarPriceLibrary.get(strDate+"-1");
		if(carPriceLibraryInnerDto1 != null) {
			price1List.add(carPriceLibraryInnerDto1.getPriceCount());
		} else {
			price1List.add("0");
		}
		
		CarPriceLibraryInnerDto carPriceLibraryInnerDto2 = CarPriceLibrary.get(strDate+"-2");
		if(carPriceLibraryInnerDto2 != null) {
			price2List.add(carPriceLibraryInnerDto2.getPriceCount());
		} else {
			price2List.add("0");
		}
		
		CarPriceLibraryInnerDto carPriceLibraryInnerDto3 = CarPriceLibrary.get(strDate+"-3");
		if(carPriceLibraryInnerDto3 != null) {
			price3List.add(carPriceLibraryInnerDto3.getPriceCount());
		} else {
			price3List.add("0");
		}
		
		CarPriceLibraryInnerDto carPriceLibraryInnerDto4 = CarPriceLibrary.get(strDate+"-4");
		if(carPriceLibraryInnerDto4 != null) {
			price4List.add(carPriceLibraryInnerDto4.getPriceCount());
		} else {
			price4List.add("0");
		}
		
		CarPriceLibraryInnerDto carPriceLibraryInnerDto5 = CarPriceLibrary.get(strDate+"-5");
		if(carPriceLibraryInnerDto5 != null) {
			price5List.add(carPriceLibraryInnerDto5.getPriceCount());
		} else {
			price5List.add("0");
		}
		
		CarPriceLibraryInnerDto carPriceLibraryInnerDto6 = CarPriceLibrary.get(strDate+"-6");
		if(carPriceLibraryInnerDto6 != null) {
			price6List.add(carPriceLibraryInnerDto6.getPriceCount());
		} else {
			price6List.add("0");
		}
		
		CarPriceLibraryInnerDto carPriceLibraryInnerDto7 = CarPriceLibrary.get(strDate+"-7");
		if(carPriceLibraryInnerDto7 != null) {
			price7List.add(carPriceLibraryInnerDto7.getPriceCount());
		} else {
			price7List.add("0");
		}
		
		CarPriceLibraryInnerDto carPriceLibraryInnerDto8 = CarPriceLibrary.get(strDate+"-8");
		if(carPriceLibraryInnerDto8 != null) {
			price8List.add(carPriceLibraryInnerDto8.getPriceCount());
		} else {
			price8List.add("0");
		}
	}
}
